package cn.ante.org.service.impl;

import cn.ante.basic.service.impl.BaseServiceImpl;
import cn.ante.org.domain.Department;
import cn.ante.org.mapper.DepartmentMapper;
import cn.ante.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    /*
    * @Title: loadAll
    * @Description: 根据高级查询的得到的集合
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/6/21 18:57
    * @Parameters: [departmentQuery]
    * @Return cn.ante.basic.utils.PageList<cn.ante.org.domain.Department>
    */


    @Override
    public void add(Department department) {
        String dirpath = "";
        departmentMapper.add(department);
        if (department.getParent() == null){
            dirpath = "/" + department.getId();
        }else{
            Department parent = departmentMapper.loadById(department.getParent().getId());
                if (parent != null){
                dirpath = parent.getDirPath() +"/" + department.getId();
                }
        }
        department.setDirPath(dirpath);
        departmentMapper.update(department);

    }

    @Override
    public void update(Department department) {
        String dirpath = "";
        if (department.getParent() == null){
            dirpath = department.getId() + "/";
        }else{
            Department parent = departmentMapper.loadById(department.getParent().getId());
            if (parent != null) {
                dirpath = parent.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirpath);
        departmentMapper.update(department);
    }


    @Override
    public List<Department> tree() {
        // 先获取到所有部门信息
        List<Department> departments = departmentMapper.loadAll();
        // new一个部门书集合
        List<Department> tree = new ArrayList<>();
        // new一个map集合，k放id,v放对象
        HashMap<Long, Department> map = new HashMap<>();
        for ( Department department:departments) {
            // 将每条部门信息遍历出来，然后封装进map集合中，是为了减少双重循环的时间
                map.put(department.getId(),department);
        }
        for ( Department departTemp:departments) {
            // 判断遍历出来的部门信息的父类id是否为空
            if (departTemp.getParentId() == null){
                // 为空则是一级，将一级部门信息直接放入树中
                tree.add(departTemp);
            }else{
                // 如果不为空，则表示该部门有父级部门，通过部门信息的parent_id找到该部门的父类
                Department department = map.get(departTemp.getParentId());
                // 找到父类对象之后，获取到父类对象中的children集合，将部门信息放到集合中去
                if (department != null) {
                    department.getChildren().add(departTemp);
                }
            }
        }
        return  tree;
    }


}
